/*
 * Copyright 2010 - Jeremie Brebec
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.jb.sharedbills.client.form;

import com.extjs.gxt.ui.client.core.XDOM;
import com.extjs.gxt.ui.client.event.PreviewEvent;
import com.extjs.gxt.ui.client.util.BaseEventPreview;
import com.extjs.gxt.ui.client.widget.BoxComponent;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.RootPanel;

public class ModalFrame extends BoxComponent {

	  private BaseEventPreview eventPreview;

	  /**
	   * Creates a new model panel.
	   */
	  public ModalFrame() {
	    baseStyle = "x-modal";
	    shim = true;
	    setShadow(false);
	    monitorWindowResize = true;
	  }

	  /**
	   * Returns the panel's event preview.
	   * 
	   * @return the event preview
	   */
	  public BaseEventPreview getEventPreview() {
	    return eventPreview;
	  }

	  /**
	   * Hides the panel.
	   */
	  public void hide() {
	    super.hide();
	    setZIndex(-1);
	    if (eventPreview != null) {
	      eventPreview.getIgnoreList().removeAll();
	      eventPreview.remove();
	    }
	    RootPanel.get().remove(this);
	  }


	  /**
	   * Displays the panel.
	   */
	  public void show() {
	    RootPanel.get().add(this);

	    el().makePositionable(true);
	    el().updateZIndex(0);

	    super.show();

	    eventPreview.getIgnoreList().removeAll();
	    eventPreview.add();

	    syncModal();
	  }

	  /**
	   * Syncs to the viewport.
	   */
	  public void syncModal() {
	    setSize(0, 0);
	    int w = XDOM.getViewWidth(true);
	    int h = XDOM.getViewHeight(true);
	    setSize(w, h);
	  }

	  @Override
	  protected void onDetach() {
	    super.onDetach();
	    if (eventPreview != null) {
	      eventPreview.remove();
	    }
	  }

	  @Override
	  protected void onRender(Element target, int index) {
	    setElement(DOM.createDiv(), target, index);
	    super.onRender(target, index);
	    eventPreview = new BaseEventPreview() {
	      @Override
	      protected boolean onPreview(PreviewEvent pe) {
	        if (pe.getEventTypeInt() == Event.ONMOUSEDOWN && getElement().isOrHasChild(pe.getTarget())
	            && (fly(pe.getTarget()).findParent(".x-ignore", -1) == null)) {
	          

	        }
	        return super.onPreview(pe);
	      }

	    };
	    eventPreview.setAutoHide(false);
	  }

	  @Override
	  protected void onWindowResize(int width, int height) {
	    super.onWindowResize(width, height);
	    syncModal();
	  }

	}
